<?php
/**
*btc列表
*/
require_once 'common.inc.php';
!in_array('3',$larr)&&adminmsg('你没有被赋予操作该项的权限！');
//$matches=parse_url($_SERVER['HTTP_REFERER']);
//$url=$matches['scheme'].'://'.$matches['host'].'/';
$rulearray=array('toip','toport','ipscope','iprule','operation','ruletype','lastmodify');
$order_arr=array( 
 'ipscope'=>array(6,3,0,0,33),
 'iprule'=>array(6,3,0,0,34) ,
 'operation'=>array(6,3,0,0,35)//开启或关闭重定向网页 
 );								//1:处于开启状态
                                //-1:处于关闭状态
if('redirectlist'==$action)
{
	$limit=5;
//	print_r( $_GET);
	$page=intval($page)<1?1:intval($page);
	$start=(intval($page)-1)*$limit;
	$sql="select count(*) from msys_rule where ruletype='$ruletype'";
	$query=$db->query($sql);
	$count=$db->fetch_array($query);
	$allpage=ceil($count['count(*)']/$limit);
	//print $allpage;
	$page=$page>$allpage?$allpage:$page;
	$url='redirect.inc.php?ruletype=1&amp;action='.$action.'&amp;';
	$pages=numofpage($count['count(*)'],$page,$allpage,$url);
	$sql="select * from msys_rule where ruletype='$ruletype' limit $start,$limit ";
	$query=$db->query($sql);
	while($redirect=$db->fetch_array($query))
	{
		if($redirect['operation']=='-1')
		{
			$redirect['operation']='开启';
		}
		elseif($redirect['operation']=='1')
		{
			$redirect['operation']='关闭';
		}
		$redirectlist[]=$redirect;
	}
	
	include_once MCC_ROOT.'/templates/msystem/redirectlist.htm';
}
elseif('redirectadd'==$action)
{
	$action='redirectdoadd';
	$redirectdb=array('toip'=>'127.0.0.1','toport'=>'80','ipscope'=>'127.0.0.1/24','iprule'=>'<>');
	include_once MCC_ROOT.'/templates/msystem/redirectadd.htm';
}
elseif('redirectdoadd'==$action)
{
	if(!$_POST['toip'])
	    adminmsg('没有指定IP','redirect.inc.php?action=redirectlist&ruletype=1');
	if(!preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/',trim($_POST['toip'])))
		adminmsg('非法IP','redirect.inc.php?action=redirectlist&ruletype=1');	
	if(!$_POST['toport'])
		adminmsg('没有指定端口','redirect.inc.php?action=redirectlist&ruletype=1');
			
	$sql='insert into msys_rule ';
	$first=0;
	$_POST['lastmodify']=time();
	$_POST['uid']=msys_uid;
	foreach($_POST as $key=>$value)
	{
		if(in_array($key,$rulearray))
		{
			if($first)
			{
				$sqlkey.=",$key";
				$sqlvalue.=",'$value'";
			}
			else
			{
				$first=1;
				$sqlkey=$sql."( $key";
				$sqlvalue=" values ('$value'";
			}
			
		}
	}
	$sql=$sqlkey.')'.$sqlvalue.')';
	if($first)
    {
		//sendInfo($reginfo,$toip,$toport);
		$db->query($sql);
		$rid=$db->insert_id();
	}
	$temppara1=str_replace(chr(0x0d).chr(0x0a),chr(7).chr(7),$ipscope);
	$reginfo1=total_order($order_arr['ipscope'][0],$order_arr['ipscope'][1],$order_arr['ipscope'][2],$order_arr['ipscope'][3],$order_arr['ipscope'][4],$temppara1,$toip);
	order_send($reginfo1);
	$temppara2=str_replace(chr(0x0d).chr(0x0a),chr(7).chr(7),$iprule);
	$reginfo2=total_order($order_arr['iprule'][0],$order_arr['iprule'][1],$order_arr['iprule'][2],$order_arr['iprule'][3],$order_arr['iprule'][4],$temppara2,$toip);
	order_send($reginfo2);	
	adminmsg('添加成功','redirect.inc.php?action=redirectlist&ruletype=1');
}
elseif('redirectedit'==$action)
{
	$action='redirectdoedit';
	$sql="select * from msys_rule where rid='$rid'";
	$query=$db->query($sql);
	$redirectdb=$db->fetch_array($query);
	include_once MCC_ROOT.'/templates/msystem/redirectadd.htm';;
}

elseif('redirectdoedit'==$action)
{
	$action='btcdoedit';
	if(!$_POST['toip'])
	    adminmsg('没有指定IP','redirect.inc.php?action=redirectedit&ruletype=1&rid='.$rid);
	if(!preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/',trim($_POST['toip'])))
		adminmsg('非法IP','redirect.inc.php?action=redirectedit&ruletype=1&rid='.$rid);		
	if(intval(!$_POST['toport']))
		adminmsg('没有指定端口','redirect.inc.php?action=redirectedit&ruletype=1&rid='.$rid);
	//$ipscope =str_replace(chr(0x0d).chr(0x0a),':',trim($ipscope));
	
	/*
	$infip = $ipscope;
	if(!empty($iprule))
	{
		$iprule =str_replace(chr(0x0d).chr(0x0a),':',trim($iprule));
		$reginfo=$ipscope.'+'.$iprule;
	}
*/
	$_POST['lastmodify']=time();
	$_POST['uid']=msys_uid;
	if($rid)
	{
		$query=$db->query("select * from msys_rule where rid='$rid' ");
		$tempdb=$db->fetch_array($query);
		
		$sql='update msys_rule set ';
		$first=0;
		foreach($_POST as $key=>$value)
		{
			if(in_array($key,$rulearray)&&isset($tempdb[$key])&&$value!=$tempdb[$key])
			{
				if($first)
				{
					$sql.=" ,$key='$value'";
				}
				else
				{
					$first=1;
					$sql=$sql."$key='$value'";
				}
				if(array_key_exists($key,$order_arr))
				{
					$temppara=str_replace(chr(0x0d).chr(0x0a),chr(7).chr(7),$value);
					$reginfo=total_order($order_arr[$key][0],$order_arr[$key][1],$order_arr[$key][2],$order_arr[$key][3],$order_arr[$key][4],$temppara,$toip);
					order_send($reginfo);
				}
				
			}
		}
		
		if($first)
		{
			//sendInfo($reginfo,$toip,$toport);
			$sql.=" where rid='$rid'";
			$db->query($sql);
		}
		
	}
	adminmsg('修改成功','redirect.inc.php?action=redirectedit&ruletype=1&rid='.$rid);
}
elseif('redirectdelete'==$action)
{
	if($rid)
	{
		$sql="delete from msys_rule where rid='$rid'";
		$db->query($sql);
		adminmsg('删除成功','redirect.inc.php?action=redirectlist&ruletype=1');
	}
	else
	{
		adminmsg('参数错误','redirect.inc.php?action=redirectlist&ruletype=1');
	}
}
//设置开启和关闭
elseif('redirectclose'==$action)
{
	if($rid)
	{
		$sql="select toip ,toport, operation from msys_rule where rid='$rid'";
		$query=$db->query($sql);
		$dip=$db->fetch_array($query);
		$toip=$dip['toip'];
		$toport=$dip['toport'];
		$key='operation';
		$temppara=$dip['operation'];
		if($temppara=='-1')
		{
			$temppara='1';
			$operation='1';
			$msg='开启成功';
		}
		elseif($temppara=='1')
		{
			$temppara='0';
			$operation='-1';
			$msg='关闭成功';
		}
		$reginfo=total_order($order_arr[$key][0],$order_arr[$key][1],$order_arr[$key][2],$order_arr[$key][3],$order_arr[$key][4],$temppara,$toip);
		order_send($reginfo);
		$sql="update msys_rule set operation='$operation' where rid='$rid'";//关闭重定向网页
		$query=$db->query($sql);
		adminmsg($msg,'redirect.inc.php?action=redirectlist&ruletype=1');
	}
	else
	   adminmsg('参数传递错误','redirect.inc.php?action=redirectlist&ruletype=1');
}
?>